package com.site.wisdom_construction.controller.iotModule;

import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.responseEntity.TwoDataResult;
import com.site.wisdom_construction.service.iotModule.FoundationWaterService;
import com.site.wisdom_construction.statisticsResult.FoundationWaterName;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/2/20
 * @Description
 */
@Api(tags = "基坑水位监测")
@RestController
@RequestMapping("FoundationWater")
@CrossOrigin
public class FoundationWaterController {
    @Autowired
    private FoundationWaterService foundationWaterService;

    @ApiOperation(value = "新增基坑水位监测数据", notes = "测试数据:")
    @PostMapping("/insertFoundationWater")
    public JsonResult insertFoundationWater(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = foundationWaterService.insertFoundationWater(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("增加成功");
            return jsonResult;
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("增加失败");
            return jsonResult;
        }
    }

    @ApiOperation(value = "统计数据", notes = "测试数据:")
    @PostMapping("/countFoundationWater")
    public TwoDataResult countFoundationWater(@RequestBody Map map) {
        TwoDataResult twoDataResult = new TwoDataResult();
        List<FoundationWaterName> foundationWaterNames = foundationWaterService.countFoundationWaterByName(map);

        for (int i = 0; i < foundationWaterNames.size(); i++) {
            String name = foundationWaterNames.get(i).getName();
            foundationWaterNames.get(i).setData(foundationWaterService.findFoundationWaterByName(name));
        }
        twoDataResult.setDataOne(foundationWaterNames);
        twoDataResult.setDataTwo(foundationWaterService.countFoundationWaterByUpTime(map));
        return twoDataResult;
    }

    @ApiOperation(value = "查询所有数据", notes = "测试数据:")
    @PostMapping("/findFoundationWater")
    public OneDataResult findFoundationWater(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(foundationWaterService.findFoundationWater(map));
        return oneDataResult;
    }

    @ApiOperation(value = "根据名称查询最新数据", notes = "测试数据:")
    @PostMapping("/findFoundationWaterByNameNew")
    public OneDataResult findFoundationWaterByNameNew(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(foundationWaterService.findFoundationWaterByNameNew(map));
        return oneDataResult;
    }

    @ApiOperation(value = "模糊分页查询基坑水位", notes = "测试数据:{\n" +
            "\t\"pageNo\": 1,\n" +
            "\t\"pageSize\": 10,\n" +
            "\t\"name\": \"\"\n" +
            "}")
    @PostMapping("/findFoundationWaterByPage")
    public Page findFoundationWaterByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(foundationWaterService.Total(map));
        page.setItems(foundationWaterService.findFoundationAxialByPage(map));
        return page;
    }


}
